package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
)

type PayFactoryController struct{}

type payFactoryParam struct {
	ID         string `json:"id"`
	Fmax       string `json:"fmax"`
	Fmin       string `json:"fmin"`
	AmountList string `json:"amount_list"`
	ShowName   string `json:"show_name"`
	Key        string `db:"key" json:"key"`
	AppKey     string `db:"app_key" json:"app_key"` //密钥
	State      string `json:"state"`
	PayRate    string `json:"pay_rate"`
	Automatic  string `json:"automatic"`
}

// List 通道列表
func (that *PayFactoryController) List(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	data, err := model.PayFactoryList(0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, data)
}

func (that *PayFactoryController) Update(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.TblPayFactory{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("修改支付通道", ctx)
	err = model.UpdatePayFactory(param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}
